package com.example.demo02.controller;

import com.example.demo02.bean.BaseReturn;
import com.example.demo02.bean.Books;
import com.example.demo02.mapper.BooksMapper;
import com.example.demo02.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @Author liuyuan
 * @Date 2022/11/8 10:44 AM
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class BookController {
    /**
     * 查询的接口
     * 添加的接口
     * 删除的接口
     * 修改的接口
     * */
//    @Autowired
//    BooksMapper booksMapper;
    @Autowired
    BooksService booksService;

    //前端axios请求的地址 http://localhost:8080/book/query
//    @RequestMapping("/book/query")
//    public BaseReturn query11232334(){
//        ArrayList<Books> query = booksMapper.query();
//        return BaseReturn.build("2000","查询成功",query);
//    }
//    @RequestMapping("/book/getById")
//    public Books getById(){
//        Books byId = booksMapper.getById(27L);
//        return byId;
//    }
    //controller 也能写业务逻辑。它写业务逻辑就没有service这层架构什么事了。
    //但是controller只做接收前端请求和响应,数据的校验。
    //service 业务层就比如 这本书要不要添加到数据，1.查询 2.插入
    //mapper(持久化到数据库)
    @RequestMapping("/book/insert")
    public BaseReturn insert(@RequestBody Books books){
        String name = books.getName();
        if(name == null){
            return BaseReturn.build("5000","请你填写书名");
        }
        boolean insert = booksService.insert(books);
        if(insert){
            System.out.println("-----添加成功-------------");
            return BaseReturn.build("2000","添加成功");
        }else{
            System.out.println("-----添加失败-------------");
            return BaseReturn.build("5000","添加失败");
        }

    }



}
